# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 5: Test and plot linearity
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(estimatr)
library(broom)
library(ggthemes)
library(directlabels)
library(scales)

# Load data ---------------------------------------------------------------

load("temp/data_for_analysis_donors.RData")
load("temp/data_for_analysis_public.RData")

df_republican_donors_linear <- df_cleaned_donors %>%
  filter(partisanship_dum == 1) %>% 
  mutate(one_violation = 0) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Stand up to other party") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Disregard politicized decisions") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Partisan involvement"), 1, one_violation)) %>% 
  mutate(two_violations = 0) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Independent"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Obey courts") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Supports compromise") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(three_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                     Compromise == c("Stand up to other party") &
                                     Investigations == c("Partisan involvement"), 1, 0)) 

df_democrat_donors_linear <- df_cleaned_donors %>%
  filter(partisanship_dum == 2) %>% 
  mutate(one_violation = 0) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Stand up to other party") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Disregard politicized decisions") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Partisan involvement"), 1, one_violation)) %>% 
  mutate(two_violations = 0) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Independent"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Obey courts") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Supports compromise") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(three_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                     Compromise == c("Stand up to other party") &
                                     Investigations == c("Partisan involvement"), 1, 0)) 

df_republican_public_linear <- df_cleaned_public %>%
  filter(partisanship_dum_7 == 1) %>% 
  mutate(one_violation = 0) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Stand up to other party") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Disregard politicized decisions") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Partisan involvement"), 1, one_violation)) %>% 
  mutate(two_violations = 0) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Independent"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Obey courts") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Supports compromise") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(three_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                     Compromise == c("Stand up to other party") &
                                     Investigations == c("Partisan involvement"), 1, 0)) 

df_democrat_public_linear <- df_cleaned_public %>%
  filter(partisanship_dum_7 == 2) %>% 
  mutate(one_violation = 0) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Stand up to other party") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Disregard politicized decisions") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Independent"), 1, one_violation)) %>% 
  mutate(one_violation = ifelse(Courts == c("Obey courts") &
                                  Compromise == c("Supports compromise") &
                                  Investigations == c("Partisan involvement"), 1, one_violation)) %>% 
  mutate(two_violations = 0) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Independent"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Obey courts") &
                                   Compromise == c("Stand up to other party") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(two_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                   Compromise == c("Supports compromise") &
                                   Investigations == c("Partisan involvement"), 1, two_violations)) %>% 
  mutate(three_violations = ifelse(Courts == c("Disregard politicized decisions") &
                                     Compromise == c("Stand up to other party") &
                                     Investigations == c("Partisan involvement"), 1, 0)) 

model <- formula(selected ~ 
                    `Gender` +
                    `Race` +
                    `Partisanship` +
                    `Taxes` +
                    `Voting` +
                    `one_violation` +
                    `two_violations` +
                    `three_violations` + 
                    age_dum +
                    education_dum +
                    gender)

republican_donors_linear_plot <- tidy(lm_robust(model, data = df_republican_donors_linear, clusters = id)) %>% 
  filter(grepl("violation", term)) %>% 
  mutate(term = factor(term, levels = c("one_violation", "two_violations", "three_violations"))) %>% 
  mutate(Subgroup = "Donors, Republican")

democrat_donors_linear_plot <- tidy(lm_robust(model, data = df_democrat_donors_linear, clusters = id)) %>% 
  filter(grepl("violation", term)) %>% 
  mutate(term = factor(term, levels = c("one_violation", "two_violations", "three_violations"))) %>% 
  mutate(Subgroup = "Donors, Democrat")

all_plot_donors <- rbind(republican_donors_linear_plot, democrat_donors_linear_plot)

republican_public_linear_plot <- tidy(lm_robust(model, data = df_republican_public_linear, clusters = id)) %>% 
  filter(grepl("violation", term)) %>% 
  mutate(term = factor(term, levels = c("one_violation", "two_violations", "three_violations"))) %>% 
  mutate(Subgroup = "Public, Republican")

democrat_public_linear_plot <- tidy(lm_robust(model, data = df_democrat_public_linear, clusters = id)) %>% 
  filter(grepl("violation", term)) %>% 
  mutate(term = factor(term, levels = c("one_violation", "two_violations", "three_violations"))) %>% 
  mutate(Subgroup = "Public, Democrat")

all_plot_public <- rbind(republican_public_linear_plot, democrat_public_linear_plot)

all_plot <- rbind(all_plot_public, all_plot_donors) %>% 
  mutate(term = factor(term, levels = c("one_violation", "two_violations", "three_violations"))) %>% 
  mutate(Subgroup = factor(Subgroup, levels = c("Donors, Democrat", "Donors, Republican", "Public, Democrat", "Public, Republican")))

all_plot2 <- all_plot %>%
  mutate(x = as.numeric(term),
         x = ifelse(Subgroup == "Donors, Democrat",   x - 0.0375, x),
         x = ifelse(Subgroup == "Donors, Republican", x - 0.0125, x),
         x = ifelse(Subgroup == "Public, Democrat",   x + 0.0125, x),
         x = ifelse(Subgroup == "Public, Republican", x + 0.0375, x))

all_plot2 %>% count(term, x)

g <- ggplot(all_plot2, aes(x = x, y = estimate, group = Subgroup, color = Subgroup)) +
  geom_point(size = 2.0) +
  geom_line() +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high)) +
  geom_dl(aes(label = Subgroup), method = list(cex = 1.25, dl.trans(x = x + .3), "last.bumpup")) +
  geom_hline(yintercept = 0, color = "darkgrey", linetype = "dashed") +
  theme_few() +
  guides(linetype = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("One", "Two", "Three")) +
  scale_y_continuous(breaks = c(-.6, -.5, -.4, -.3, -.2, -.1, 0), labels = percent_format()) +
  scale_color_manual(values = c("dodgerblue4", "firebrick4", "dodgerblue", "firebrick1")) +
  labs(title = NULL,
       y = "Estimated marginal effect", 
       x = "Number of democratic norm violations") +
  theme(plot.title = element_text(size = 18, colour = "black"),
        legend.title = element_text(size = 16, colour = "black"),
        axis.text.x =  element_text(size = 14, colour = "black", hjust = .5, vjust = 1),
        axis.text.y =  element_text(size = 14, colour = "black", hjust = 0, vjust = 0),
        axis.title.y = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        strip.text.x = element_text(size = 16, margin = margin(b = 5)),
        legend.position = "none") +
  coord_cartesian(xlim = c(1, 3.5))

g

ggsave(paste0("figures/linearity_with_CIs.pdf"), 
       plot = g, width = 10, height = 6)